package top.zackyoung.mq.rocketmq.demo;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * @version 1.0
 * @description: 生产者
 * @author: ZackYoung
 * @date: 2021/4/3 11:40
 */
public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        // 创建消息生产者，并指定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("myproducer-group");
        // 设置NameServer地址
        producer.setNamesrvAddr("192.168.31.138:9876");
        // 启动生产者
        producer.start();
        // 创建消息对象
        Message message = new Message("myTopic", "myTag", ("myMessage").getBytes());
        // 发送消息，设置超时时间
        SendResult result = producer.send(message, 10000);
        System.out.println(result);
        // 关闭生产者
        producer.shutdown();
    }
}
